﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sucdri.Domain.Model;
using Sucdri.Infrastructure.Core;
using Sucdri.Infrastructure.Core.DbUtility;

namespace Sucdri.Domain.Repository.DataMapper
{
   internal abstract class BaseDataMapper : IDataMapper
   {
      protected abstract string GetInsertCommand();
      protected abstract string GetUpdateCommand();
      protected abstract string GetDeleteCommand();
      protected abstract IDictionary<string, object> BuildInsertParameter(DomainEntity entity);
      protected abstract IDictionary<string, object> BuildUpdateParameter(DomainEntity entity);
      protected abstract IDictionary<string, object> BuildDeleteParameter(DomainEntity entity);
      protected readonly DbHelper DbHelper;

      protected BaseDataMapper()
      {
         DbHelper = DbFactory.GetDbHelper();
      }

      public void Create(DomainEntity entity)
      {
         Guard.ArgumentNotNull("entity", entity);
         DbHelper.ExecuteInsert(GetInsertCommand(), BuildInsertParameter(entity));
      }

      public void Update(DomainEntity entity)
      {
         Guard.ArgumentNotNull("entity", entity);
         DbHelper.ExecuteNonQuery(GetUpdateCommand(), BuildUpdateParameter(entity));
      }

      public void Delete(DomainEntity entity)
      {
         Guard.ArgumentNotNull("entity", entity);
         DbHelper.ExecuteNonQuery(GetDeleteCommand(), BuildDeleteParameter(entity));
      }
   }
}
